package top.i51213.fastaction.png;

import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;

public class Util {
    public static byte[] int2Buff(int i)
    {
        return new byte[] { (byte)((i >> 24) & 0xff), (byte)((i >> 16) & 0xff), (byte)((i >> 8) & 0xff), (byte)(i & 0xff) };
    }
    public static byte[] link(byte[] ...buffs)
    {
        int len = 0;
        for(byte[] buff:buffs)
        {
            len += buff.length;
        }
        byte[] vs = new byte[len];
        int index = 0;
        for(byte[] buff:buffs)
        {
            System.arraycopy(buff, 0, vs, index, buff.length);
            index += buff.length;
        }
        return vs;
    }
    public static byte[] bytes2Buff(int ...bs)
    {
        byte[] buff = new byte[bs.length];
        for (int i = 0; i < bs.length; i++)
        {
            buff[i] = (byte)bs[i];
        }
        return buff;
    }
    public static void argb2rgba(int[] pixels,int srcPos,int count,byte[] dest,int offset){
        for (int i = srcPos,p,x; i < count; i++) {
            p=offset+4*i;
            x=pixels[i];
            dest[p]= (byte) ((x>>16)&0xff);
            dest[p+1]= (byte) ((x>>8)&0xff);
            dest[p+2]= (byte) ((x)&0xff);
            dest[p+3]= (byte) (((x)>>24)&0xff);
        }
    }

    public static byte[] getCrc32(String s,byte[] buff){
        return getCrc32(s, buff,0,buff.length);
    }
    public static byte[] getCrc32(String s,byte[] buff,int offset,int count){
        CRC32 c = new CRC32();
        c.update(s.getBytes(StandardCharsets.US_ASCII));
        c.update(buff,offset,count);
        long v=c.getValue();
        return new byte[] { (byte)((v >> 24) & 0xff), (byte)((v >> 16) & 0xff), (byte)((v >> 8) & 0xff), (byte)(v & 0xff) };
    }
}
